<template>
  <div>
    <!-- Breadcrumb Navigation area -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">Home</el-breadcrumb-item>
      <el-breadcrumb-item>Goods</el-breadcrumb-item>
      <el-breadcrumb-item>List</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- card view area -->
    <el-card>
      <!-- Search & add area -->
      <el-row :gutter="13">
        <el-col :span="7">
          <el-input
            placeholder="Please enter serch-key"
            clearable
            v-model="queryInfo.query"
            @clear="getGoodsList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">Add Goods</el-button>
        </el-col>
      </el-row>

      <!-- Goods list -->
      <el-table :data="goodsList" style="width: 100%" border stripe>
        <el-table-column type="expand" label="-"></el-table-column>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="Goods Name"></el-table-column>
        <el-table-column prop="goods_price" label="Price(￥)" width="97px"></el-table-column>
        <el-table-column prop="goods_weight" label="Weight" width="73px"></el-table-column>
        <el-table-column label="Create Time" width="170px">
          <template slot-scope="sp">{{sp.row.add_time | dateFormat}}</template>
        </el-table-column>
        <el-table-column label="Operating" width="125px">
          <template slot-scope="sp">
            <!-- edit btn -->
            <el-tooltip
              class="item"
              effect="dark"
              :content="'Edit ' + sp.row.goods_name"
              placement="top-start"
              :enterable="false"
            >
              <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            </el-tooltip>
            <!-- deltet btn -->
            <el-tooltip
              class="item"
              effect="dark"
              :content="'Delete ' + sp.row.goods_name"
              placement="top-start"
              :enterable="false"
            >
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="deleteById(sp.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- paging -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 3, 5, 10, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  created() {
    this.getGoodsList()
  },
  data() {
    return {
      addDialogVisible: false,
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      goodsList: [],
      total: 0
    }
  },
  methods: {
    async getGoodsList() {
      const rs = await this.$http.get('goods', { params: this.queryInfo })
      if (rs.meta.status !== 200) {
        return this.$msg.error('get goods list faild!')
      }
      this.goodsList = rs.data.goods
      this.total = rs.data.total
      // this.$msg.success('get goods list success!')
      // console.log(JSON.stringify(this.goodsList))
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    async deleteById(goods) {
      /* Ask the user before deleting */
      /* if user cancel operation: result is  Promise Objest
       * so need catch to get error message & return
       *
       * if user confirm: result is a String
       */
      const confirmResult = await this.$confirm(
        'You will be deleting {' +
          goods.goods_name +
          '} Goods, the operation unrecoverable, you sure?',
        'Warning',
        {
          confirmButtonText: 'I Sure',
          cancelButtonText: 'Cancel',
          type: 'warning'
        }
      ).catch(err => err)

      /* if user cancel: tip & do noting */
      if (confirmResult !== 'confirm') {
        return this.$msg.info('Canceled')
      }
      /* if user confirm: do it! */
      const rs = await this.$http.delete(`/goods/${goods.goods_id}`)
      if (rs.meta.status !== 200) {
        return this.$msg.error('delete ' + goods.goods_name + ' goods faild!')
      }
      this.$msg.success(`deletd ${goods.goods_name} goods`)
      this.getGoodsList()
    },
    goAddPage() {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style>
</style>
